#ifndef _SFTMR_H_
#define _SFTMR_H_
#include <stddef.h>
#include "config.h"


// Types of timer-tick
typedef uint32_t sftmr_tk_t;

// Timer structure
typedef struct _sftmr_t {
    struct _sftmr_t *next;
    void (*handle_func)(void *); // callback function
    void *arg; // Callback the input parameters
	sftmr_tk_t reload;
	sftmr_tk_t timeout;

    uint16_t malloc_type:1; // timer is static or not
    uint16_t is_running:1; // timer is running or not
    uint16_t is_isr:1; // timer is used in ISR or not
    uint16_t resv:13; // reserved
}sftmr_t;


void sftmr_process(void);

// create a new timer and add it to the list
// if static_timer is NULL, it will malloc a new timer
// if static_timer is not NULL, it will use the static_timer as the timer
// return the timer if successful, NULL if failed
sftmr_t* sftmr_create(sftmr_t *static_timer, void (*handler)(void *), void *arg);

// delete a timer from the list and free the memory
// return 0 if successful, -1 if failed
int sftmr_delete(sftmr_t *timer);

// start a timer with a timeout and an reload
void sftmr_start(sftmr_t* timer, sftmr_tk_t timeout, sftmr_tk_t reload);

// stop a timer
void sftmr_stop(sftmr_t* timer);

#endif /* _SFTMR_H_ */
